<?php

namespace App\Http\Controllers\Admin\Speechcrafts;

use App\Http\Controllers\Controller;
use App\Models\StoreServiceFeedback;
use App\Traits\Controllers\DestroyTraits;
use App\Traits\Controllers\IndexTraits;
use App\Traits\Controllers\ShowTraits;
use App\Traits\Controllers\UpdateStatusTraits;
use App\Transformers\Store\StoreServiceFeedbackTransformer;
use Carbon\Carbon;
use Illuminate\Http\Request;

class StoreServiceFeedbackController extends Controller
{
    //客服反馈表的查和删
    use IndexTraits;
    use DestroyTraits;
    use ShowTraits;
    use UpdateStatusTraits;

    //定义模型和transformer
    public $model = StoreServiceFeedback::class;
    public $transformer = StoreServiceFeedbackTransformer::class;


    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'PATCH':

            case 'POST':
                return [
                    'content' => 'required|min:10|max:255',
                    'name' => 'required|min:1|max:20',
                ];
                break;
            case 'PUT':
                return [
                    'make' => 'required|min:10|max:255',
                ];
                break;
        }
        return [

        ];
    }


    public function ruleMessages(): array
    {
        return [
            'make.required' => '备注的内容 不能为空',
            'make.min' => '备注的内容 不能少于10个字段',
            'make.max' => '备注的内容 不能多于255个字段',
            'content.required' => '反馈的内容 不能为空',
            'content.min' => '反馈的内容 不能少于10个字段',
            'content.max' => '反馈的内容 不能多于255个字段',
            'name.required' => '姓名 不能为空',
            'name.min' => '姓名 不能少于1个字段',
            'name.max' => '姓名 不能多于20个字段',
        ];
    }

    public function index(Request $request)
    {

        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);

        $product_name = $request->input('user_name');
        $request->offsetUnset('user_name');

        $start_time = $request->input('start_time');
        $end_time = $request->input('end_time');

        //获取传过来的时间类型
        $time = $request->input('time');


        $data = $this
            ->getQuery()
            ->pimp()//自动转换where条件
            ->when($time || ($start_time && $end_time), function ($query) use ($time,$start_time,$end_time) {
                //自定义时间
                if(!empty($start_time) && !empty($end_time)){
                    $query->whereBetween('created_at',[$start_time,$end_time]);
                }

                //搜索今天数据
                if ($time == "today") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfDay(), Carbon::now()->endOfDay()]);
                }

                //搜索昨天数据
                if ($time == "yesterday") {
                    $query->whereDay('created_at', [Carbon::now()->yesterday(), Carbon::now()->startOfDay()]);
                }

                //搜索最近一周数据
                if ($time == "week") {
                    $query->whereBetween('created_at', [Carbon::now()->subDays(7), Carbon::now()->subDays(0)]);
                }

                //搜索最近三十天数据
                if ($time == "thirty") {
                    $query->whereBetween('created_at', [Carbon::now()->subDays(30), Carbon::now()->subDays(0)]);
                }

                //搜索本月数据
                if ($time == "month") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()]);
                }

                //搜索本年数据
                if ($time == "year") {
                    $query->whereBetween('created_at', [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()]);
                }
            })
            ->paginate($this->getPageLimit());

        return $this->response->paginator($data, resolve($this->transformer));

    }
}
